import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue';
import {
	log
} from 'util'
export default {
	props: {
		mustIcon: {
			type: String,
			default: require('@/static/Scope.png'),
		},
		unIcon: {
			type: String,
			default: require('@/static/unscope.png'),
		}
	},
	data() {

		return {
			Inv: 5,
			
			
			// 详情
			totalData: {
				lrCheckSample: {
					fSampletypename: "", //样品种类汉字
					fSamplemodel: "", //样品编码
					fSamplereportnostr: "", //报告编号
					fReportdates: "", //报告日期
					fProjectlocation: "", //工程部位/用途
				},
				checkSampletestclose: {
					fId: "", // 申请单fid //为空判断为新增
					fCompanyid: "", // 施工标段id
					fParentid: "", // 对应的超标记录fid
					fWarningcontent: "", // 不合格参数及结果
					fReason: "", // 问题原因
					fHandleway: "", // 处理方式
					fHandleresult: "", // 处理结果
					fHandledate: "",
					fHandledates: "", // 处理日期
					fHandleusername: "", // 处理人名称
					fHandlefile: "", // 附件

					fFirstcheckresult: "", // 一级审核处理结果
					fFirstcheckopinion: "", // 一级审核处理意见
					fFirstcheckusername: "", // 一级审核处理人name
					fFirstcheckdates: "", // 一级审核处理日期
					fFirstcheckfile: "", // 一级审核附件

					fSecondcheckresult: "", // 二级审核处理结果
					fSecondcheckopinion: "", // 二级审核处理意见
					fSecondcheckusername: "", // 二级审核处理人name
					fSecondcheckdates: "", // 二级审核处理日期
					fSecondcheckfile: "", // 二级审核附件

					aduitStatus: "", // 保存状态
					userId: "", // 当前登录人id
					userName: "", // 当前登录人姓名
					recordfiles: "", // 删除文件id集合
				},
			}, //详情数据
			Fdata: {}, //上个页面传过来的参数

			// 闭合
			companyNum: null, //判断用户身份
			timestamp: null, //对应附件id
			timestamp1: null,
			timestamp2: null,
			must0: "must", //必填
			must1: "must",
			must2: "must",
			readonly0: false, //是否只读
			readonly1: false,
			readonly2: false,
			closeNum: -1, //闭合处置结果
			FirstNum: -1,
			SecondNum: -1,
			handle1: ["同意", "不同意"],
			flagNum: -1, //日期选择是否可选
			// 闭合
			// 详情
			
			


			// 文件上传
			count: 8,
			instantly: true, // 选择文件后是否立即自动上传
			debug: true,
			size: 10,
			heights: '156rpx',
			enclosureImg: {},
			option: {},
			fPhotofilelist: [], //处理过程相关资料
			FauditFileurllist: [], //一级审核材料List
			SauditFileurllist: [], //二级审核材料List
			recordfiles: [], //删除合集
			fFileName: "",
			pdfUrl: "",
			// 文件上传



			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式



			userInfo: null, //当前登录用户
			timing: null, //当前年月日
		};
	},



	onLoad(options) {
		uni.getStorage({
			key: 'userInfo',
			success: function(res) {
				this.userInfo = JSON.parse(res.data);
			}
		});

		this.getFdata(options);
		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		// this.playVideo();
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		this.appliance = 1
		// #endif
	},
	onShow() {

	},
	onReady() {
	},
	components: {
		loading,
		cLabel,
		lsjUpload,
		fromItem
	},

	onUnload() {
		uni.$off('drawer-page')
	},

	methods: {
		// 切换表头
		yj_searchForm(num) {
			var that = this;
			this.Inv = num;
		},
		getFdata(data) {
			this.Fdata = data;
			this.companyNum = this.Fdata.Look;
			this.Inv = this.Fdata.Look;

			if (this.Fdata.Look == "0") {
				this.readonly0 = false;
				this.readonly1 = true;
				this.readonly2 = true;
				this.must0 = "must";
				this.must1 = "must";
				this.must2 = "must";
				this.closeNum = -1;
				this.FirstNum = -1
				this.SecondNum = -1
				// this.activeNames = ["1"];
			} else if (this.Fdata.Look == "1") {
				this.readonly0 = true;
				this.readonly1 = false;
				this.readonly2 = true;
				this.must0 = "no";
				this.must1 = "must";
				this.must2 = "must";
				this.closeNum = 0
				this.FirstNum = -1
				this.SecondNum = -1
				// this.activeNames = ["1", "2"];

			} else if (this.Fdata.Look == "2") {
				this.readonly0 = true;
				this.readonly1 = true;
				this.readonly2 = false;
				this.must0 = "no";
				this.must1 = "no";
				this.must2 = "must";
				this.closeNum = 0
				this.FirstNum = 0
				this.SecondNum = -1
				// this.activeNames = ["1", "3"];
			} else if (this.Fdata.Look == "3") {
				this.readonly0 = true;
				this.readonly1 = true;
				this.readonly2 = true;
				this.must0 = "no";
				this.must1 = "no";
				this.must2 = "no";
				this.FirstNum = 0
				this.closeNum = 0
				this.SecondNum = 0
				// this.activeNames = ["1"];
			}
			this.getDetailData(this.Fdata.id, this.Fdata.Look);
		},
		getDetailData(id, num) {
			this.getDate();

			this.$http.getSampleWorkByFid({
					fId: id
				})
				.then((res) => {
					if (res.errcode == 0) {
						var that = this;
						that.fileList = [];
						that.recordfiles = [];
						that.totalData = res.data;
						uni.getStorage({
							key: 'userInfo',
							success: function(ress) {
								that.userInfo = JSON.parse(ress.data);
								if (num == "0") {
									// 处理日期
									if (that.totalData.checkSampletestclose.fHandledates == null || that
										.totalData
										.checkSampletestclose.fHandledates == "") {
										that.totalData.checkSampletestclose.fHandledates = that.timing;
									}
									// 处理人名称
									if (that.totalData.checkSampletestclose.fHandleusername == "" ||
										that.totalData
										.checkSampletestclose.fHandleusername == null) {
										that.totalData.checkSampletestclose.fHandleusername = that
											.userInfo.realName;
									}
								}

								if (num == "1") {
									// 处理日期
									if (that.totalData.checkSampletestclose.fFirstcheckdates == null ||
										that.totalData
										.checkSampletestclose.fFirstcheckdates == "") {
										that.totalData.checkSampletestclose.fFirstcheckdates = that
											.timing;
									}
									// 处理人名称
									if (that.totalData.checkSampletestclose.fFirstcheckusername ==
										null || that.totalData
										.checkSampletestclose.fFirstcheckusername == "") {
										that.totalData.checkSampletestclose.fFirstcheckusername = that
											.userInfo.realName;
									}
								}

								if (num == "2") {
									// 处理日期
									if (that.totalData.checkSampletestclose.fSecondcheckdates == null ||
										that.totalData
										.checkSampletestclose.fSecondcheckdates == "") {
										that.totalData.checkSampletestclose.fSecondcheckdates = that
											.timing;
									}
									// 处理人名称
									if (that.totalData.checkSampletestclose.fSecondcheckusername ==
										null || that.totalData
										.checkSampletestclose.fSecondcheckusername == "") {
										that.totalData.checkSampletestclose.fSecondcheckusername = that
											.userInfo.realName;
									}
								}

							}
						});

						// 文件
						that.timestamp = that.totalData.checkSampletestclose.fHandlefile; //申请单文件id
						that.timestamp1 = that.totalData.checkSampletestclose.fFirstcheckfile; //一级审核文件id
						that.timestamp2 = that.totalData.checkSampletestclose.fSecondcheckfile; //二级审核文件id
						that.fPhotofilelist = that.totalData.fCloseSourcefileList; //闭合申请单材料List
						this.FauditFileurllist = res.data.fFirstSourcefileList; //一级审核材料List
						this.SauditFileurllist = res.data.fSecondSourcefileList; //二级审核材料List
						
						
						
						// 上传文件
						var id = null,
							list = null;
						if (this.Fdata.Look == "0") {
							id = this.timestamp;
							this.$nextTick(() => {
								this.$refs.lsjUpload.show();
							})
						} else if (this.Fdata.Look == "1") {
							id = this.timestamp1;
							this.$nextTick(() => {
								this.$refs.lsjUpload1.show();
							})
						} else if (this.Fdata.Look == "2") {
							id = this.timestamp2;
							this.$nextTick(() => {
								this.$refs.lsjUpload2.show();
							})
						}

						var that = this
						if (id == "" && id == null && id == undefined) {
							console.log("文件没准备好")
						} else {
							setTimeout(() => {
								uni.getStorage({
									key: 'userInfo',
									success: function(res) {
										that.userInfo = JSON.parse(res.data);
										var folderid = id;
										var userId = that.userInfo.userId;
										var userName = that.userInfo.realName;
										var url = that.$http.upload + '?folderid=' + folderid +
											'&userId=' +
											userId + '&userName=' + userName;
										that.option = {
											// 上传服务器地址，此地址需要替换为你的接口地址
											url: url,
											// 上传附件的key
											name: 'file',
											header: {},
											formData: {}
										};
									}
								});
							}, 3000)
						}
						// 上传文件
					} else {
						uni.showToast({
							title: '列表页接口报错',
							icon: 'none',
							duration: 2000
						});
						return false;
					}
				})
				.catch((error) => {
					console.log("列表页接口报错");
					return false;
				});
		},

		// 保存按钮
		clickNum(data) {
			this.saveData();
		},

		// 判断必填项
		saveData() {
			var that = this;
			if (
				(that.totalData.checkSampletestclose.fHandleway == "" ||
					that.totalData.checkSampletestclose.fHandleway == undefined) &&
				that.companyNum == "0"
			) {
				uni.showToast({
					title: '闭合申请单 处理方式不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fHandleusername == "" ||
					that.totalData.checkSampletestclose.fHandleusername == undefined) &&
				that.companyNum == "0"
			) {
				uni.showToast({
					title: '闭合申请单 处理人不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fHandledates == "" ||
					that.totalData.checkSampletestclose.fHandledates == undefined) &&
				that.companyNum == "0"
			) {
				uni.showToast({
					title: '闭合申请单 处理日期不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fFirstcheckresult == "" ||
					that.totalData.checkSampletestclose.fFirstcheckresult == undefined) &&
				that.companyNum == "1"
			) {
				uni.showToast({
					title: '一级审核 审核结果不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fFirstcheckusername == "" ||
					that.totalData.checkSampletestclose.fFirstcheckusername == undefined) &&
				that.companyNum == "1"
			) {
				uni.showToast({
					title: '一级审核 审核人不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fFirstcheckdates == "" ||
					that.totalData.checkSampletestclose.fFirstcheckdates == undefined) &&
				that.companyNum == "1"
			) {
				uni.showToast({
					title: '一级审核 审核日期不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fSecondcheckresult == "" ||
					that.totalData.checkSampletestclose.fSecondcheckresult == undefined) &&
				that.companyNum == "2"
			) {
				uni.showToast({
					title: '二级审核 审核结果不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fSecondcheckusername == "" ||
					that.totalData.checkSampletestclose.fSecondcheckusername == undefined) &&
				that.companyNum == "2"
			) {
				uni.showToast({
					title: '二级审核 审核人不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (
				(that.totalData.checkSampletestclose.fSecondcheckdates == "" ||
					that.totalData.checkSampletestclose.fSecondcheckdates == undefined) &&
				that.companyNum == "2"
			) {
				uni.showToast({
					title: '二级审核 审核日期不能为空',
					icon: 'none',
					duration: 2000
				});

				return false;
			} else {
				that.clickN = 1
				let totalData1 = {
					fId: "", // 申请单fid //为空判断为新增
					fCompanyid: this.Fdata.fCompanyid,
					aduitStatus: that.companyNum, // 保存状态
					fParentid: that.Fdata.id, // 对应的超标记录fid
					fWarningcontent: that.totalData.checkSampletestclose.fWarningcontent, // 预警内容
					fReason: that.totalData.checkSampletestclose.fReason, // 问题原因
					fHandleway: that.totalData.checkSampletestclose.fHandleway, // 处理方式
					fHandleresult: that.totalData.checkSampletestclose.fHandleresult, // 处理结果
					fHandledate: "",
					fHandledates: that.totalData.checkSampletestclose.fHandledates, // 处理日期
					fHandleusername: that.totalData.checkSampletestclose.fHandleusername, // 处理人名称
					fHandlefile: that.totalData.checkSampletestclose.fHandlefile, // 附件

					fFirstcheckresult: that.totalData.checkSampletestclose.fFirstcheckresult, // 一级审核处理结果
					fFirstcheckopinion: that.totalData.checkSampletestclose.fFirstcheckopinion, // 一级审核处理意见
					fFirstcheckusername: that.totalData.checkSampletestclose.fFirstcheckusername, // 一级审核处理人name
					fFirstcheckdates: that.totalData.checkSampletestclose.fFirstcheckdates, // 一级审核处理日期
					fFirstcheckfile: that.totalData.checkSampletestclose.fFirstcheckfile, // 一级审核附件


					fSecondcheckresult: that.totalData.checkSampletestclose.fSecondcheckresult, // 二级审核处理结果
					fSecondcheckopinion: that.totalData.checkSampletestclose.fSecondcheckopinion, // 二级审核处理意见
					fSecondcheckusername: that.totalData.checkSampletestclose.fSecondcheckusername, // 二级审核处理人name
					fSecondcheckdates: that.totalData.checkSampletestclose.fSecondcheckdates, // 二级审核处理日期
					fSecondcheckfile: that.totalData.checkSampletestclose.fSecondcheckfile, // 二级审核附件

					userId: that.userInfo.userId, // 当前登录人id
					userName: that.userInfo.realName, // 当前登录人姓名
					recordfiles: that.recordfiles, // 删除文件id集合
				};

				if (that.totalData.checkSampletestclose.fId == null) {
					totalData1.fId = "";
				} else {
					totalData1.fId = that.totalData.checkSampletestclose.fId;
				}

				this.$http.addClosedFormLedger(totalData1)
					.then((res) => {
						if (res.errcode == 0) {
							that.$refs.loading.open();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/testDetectionManage/unqualifiedReport/testing/index?Inv=" +
											0,
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)

											// #endif

										}

									})
								}, 1500)
							}
							that.click = true
						} else {
							uni.showToast({
								title: "保存失败",
								icon: 'none',
								duration: 2000
							});
							that.clickN = 0
							this.$refs.loading.close();
						}
					})
			}
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			//   const hh = (time.getHours() + "").padStart(2, "0");
			//   const mm = (time.getMinutes() + "").padStart(2, "0");
			//   const ss = (time.getSeconds() + "").padStart(2, "0");

			//   var timer = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
			this.timing = `${y}-${m}-${d}`;
		},
		selectorFun(e) {},
		
		// 原生窗体按钮显示
		// playVideo() {
		// 	let subNVue = uni.getSubNVueById('safe_notice')
		// 	subNVue.show()
		// },
		callback() {},




		// 上传文件
		// 上传
		onprogressfRecheckfileList(item) {
			if (item.responseText) {
				var list = null;
				if (this.Fdata.Look == "0") {
					list = this.fPhotofilelist; //处理过程相关资料
				} else if (this.Fdata.Look == "1") {
					list = this.FauditFileurllist; //一级审核材料List
				} else if (this.Fdata.Look == "2") {
					list = this.SauditFileurllist; //二级审核材料List
				}

				var data = JSON.parse(item.responseText)
				var url = data[0].atta.fFilepath
				var urlPath;
				if (url.indexOf("D:") != -1) {
					urlPath = url.replace(this.$http.redf, this.$http.file)
				}
				list.push({
					url: urlPath,
					name: item.name,
					fId: data[0].atta.fId
				});
			}
		},
		// 删除
		DelFilefRecheckfileList(e, id, index, list) {
			var that = this
			uni.showModal({
				title: '召唤师',
				content: '确定要删除这段回忆吗？',
				cancelText: '再看看',
				confirmText: '再见',
				success: res => {
					if (res.confirm) {
						that.recordfiles.push({
							fId: id
						})
						list.splice(index, 1);
						// this.fPhotofilelist.splice(index, 1);
					}
				}
			})
		},
		// 预览
		pdfPage(name, url) {
			let links = encodeURIComponent(url)
			uni.navigateTo({
				url: '/pages/apply/pdf?links=' + links + '&names=' + name,
			})
		},
		// 文件选择回调
		onChange(files) {},
		// 上传文件


	}
}
